package com.jida.tijian.service;

import com.jida.tijian.domain.Checkitem;
import com.jida.tijian.domain.Setmeal;
import com.jida.tijian.domain.Setmealdetailed;
import com.jida.tijian.dto.ResponseObject;
import com.jida.tijian.mapper.CheckitemMapper;
import com.jida.tijian.mapper.CheckitemdetailedMapper;
import com.jida.tijian.mapper.SetmealMapper;
import com.jida.tijian.mapper.SetmealdetailedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.websocket.OnError;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealdetailedMapper setmealdetailedMapper;

    @Autowired
    CheckitemMapper checkitemMapper;
    //根据性别获取套餐列表数据，同时把每个套餐关联的检查项也检查出来
    @Override
    public ResponseObject listSetmeal(Integer sex){

        //1.首先根据性别查询所有对应的套餐数据
        List<Setmeal> setmealList=setmealMapper.selectBySex(sex);

        //2.拿着套餐编号查询都有哪些检查项编号，再根据检查项编号查询检查项的详细内容
        for (int i = 0; i < setmealList.size(); i++) {

            Setmeal setmeal = setmealList.get(i);//拿到每个套餐

            List<Setmealdetailed> setmealdetailedList = setmealdetailedMapper.selectBySetmealId(setmealList.get(i).getSmId());

            //定义一个容器：存放当前套餐的对应的检查项集合
            List<Checkitem> checkitemList= new ArrayList<>();

            for(Setmealdetailed sd:setmealdetailedList){
                Integer ciId=sd.getCiId();
                Checkitem checkitem = checkitemMapper.selectByPrimaryKey((ciId));
                checkitemList.add(checkitem);
            }
            setmeal.setCheckitemList((checkitemList));

        }
        //打印输出一下

        return new ResponseObject(ResponseObject.SUCCESS_STATUS,ResponseObject.SUCCESS_DESC,setmealList);
    }

    @Override
    public Setmeal loadSetmeal(Setmeal setmeal) {
        return setmealMapper.selectByPrimaryKey(setmeal.getSmId());
    }


}

